<template>
  <div>
    <van-cell title="搜索历史" value="内容">
      <div v-if="isDeleteShow">
        <!-- 子传父 -->
        <span @click="$emit('clear')">全部删除</span> &nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon v-else name="delete" @click="isDeleteShow = true" />
    </van-cell>
    <van-cell
      :title="item"
      value="内容"
      v-for="(item, index) in searchHistories"
      :key="index"
      @click="searchItemClick(item, index)"
    >
      <van-icon name="close" v-show="isDeleteShow" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  components: {},
  props: {
    searchHistories: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      isDeleteShow: false, // 控制元素的显示隐藏
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    searchItemClick(item, index) {
      if (this.isDeleteShow) {
        // 执行删除逻辑
        // 如果是普通数据（数字、字符串。布尔值）绝对不能修改，改了也不会传给父组件
        // 如果props 的值是一个引用数据类型，可以修改，但不能重新赋值，实际上地址值没有改变，不会影响
        this.searchHistories.splice(index, 1)
      } else {
        // 执行搜索逻辑  直接触发父组件的onsearch时间
        this.$emit('search', item)
      }
    },
  },
}
</script>

<style lang="less" scoped></style>
